package com.google.android.material.bottomsheet;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build.VERSION;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent;
import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.annotation.VisibleForTesting;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.coordinatorlayout.widget.CoordinatorLayout.Behavior;
import androidx.coordinatorlayout.widget.CoordinatorLayout.a;
import androidx.core.view.ViewCompat;
import androidx.customview.view.AbsSavedState;
import com.huawei.hwphy2d.physical.util.BarrierParameters;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import o.aqu.n;
import o.gr;
import o.jf;
import o.jf.e;

public class BottomSheetBehavior<V extends View> extends Behavior<V> {
    int ʻ;
    private int ʻॱ;
    int ʼ;
    private VelocityTracker ʼॱ;
    jf ʽ;
    private int ʽॱ;
    private boolean ʾ;
    private Map<View, Integer> ʿ;
    private c ˈ;
    private final e ˉ = new e(this) {
        final /* synthetic */ BottomSheetBehavior ˊ;

        {
            this.ˊ = r1;
        }

        public boolean ˏ(@NonNull View view, int i) {
            if (this.ˊ.ˎ == 1 || this.ˊ.ˏॱ) {
                return false;
            }
            if (this.ˊ.ˎ == 3 && this.ˊ.ʻ == i) {
                View view2 = (View) this.ˊ.ᐝ.get();
                if (view2 != null && view2.canScrollVertically(-1)) {
                    return false;
                }
            }
            boolean z = this.ˊ.ॱॱ != null && this.ˊ.ॱॱ.get() == view;
            return z;
        }

        public void ॱ(@NonNull View view, int i, int i2, int i3, int i4) {
            this.ˊ.ˎ(i2);
        }

        public void ˏ(int i) {
            if (i == 1) {
                this.ˊ.ˏ(1);
            }
        }

        public void ˎ(@NonNull View view, float f, float f2) {
            int i = 0;
            int i2 = 6;
            int i3 = 3;
            if (f2 < 0.0f) {
                if (this.ˊ.ˋॱ) {
                    i = this.ˊ.ˋ;
                } else {
                    if (view.getTop() > this.ˊ.ˊ) {
                        i3 = this.ˊ.ˊ;
                    } else {
                        i2 = 3;
                        i3 = 0;
                    }
                    i = i3;
                    i3 = i2;
                }
            } else if (this.ˊ.ॱ && this.ˊ.ˊ(view, f2) && (view.getTop() > this.ˊ.ˏ || Math.abs(f) < Math.abs(f2))) {
                i = this.ˊ.ʼ;
                i3 = 5;
            } else if (f2 == 0.0f || Math.abs(f) > Math.abs(f2)) {
                int top = view.getTop();
                if (this.ˊ.ˋॱ) {
                    if (Math.abs(top - this.ˊ.ˋ) < Math.abs(top - this.ˊ.ˏ)) {
                        i = this.ˊ.ˋ;
                    } else {
                        i = this.ˊ.ˏ;
                        i3 = 4;
                    }
                } else if (top < this.ˊ.ˊ) {
                    if (top >= Math.abs(top - this.ˊ.ˏ)) {
                        i = this.ˊ.ˊ;
                        i3 = 6;
                    }
                } else if (Math.abs(top - this.ˊ.ˊ) < Math.abs(top - this.ˊ.ˏ)) {
                    i = this.ˊ.ˊ;
                    i3 = 6;
                } else {
                    i = this.ˊ.ˏ;
                    i3 = 4;
                }
            } else {
                i = this.ˊ.ˏ;
                i3 = 4;
            }
            if (this.ˊ.ʽ.ˋ(view.getLeft(), i)) {
                this.ˊ.ˏ(2);
                ViewCompat.ˏ(view, new d(this.ˊ, view, i3));
                return;
            }
            this.ˊ.ˏ(i3);
        }

        public int ˋ(@NonNull View view, int i, int i2) {
            return gr.ॱ(i, this.ˊ.ʼ(), this.ˊ.ॱ ? this.ˊ.ʼ : this.ˊ.ˏ);
        }

        public int ˎ(@NonNull View view, int i, int i2) {
            return view.getLeft();
        }

        public int ˎ(@NonNull View view) {
            if (this.ˊ.ॱ) {
                return this.ˊ.ʼ;
            }
            return this.ˊ.ˏ;
        }
    };
    int ˊ;
    private int ˊॱ;
    int ˋ;
    private boolean ˋॱ = true;
    int ˎ = 4;
    int ˏ;
    boolean ˏॱ;
    private boolean ͺ;
    boolean ॱ;
    private float ॱˊ;
    private boolean ॱˋ;
    private int ॱˎ;
    WeakReference<V> ॱॱ;
    private boolean ॱᐝ;
    WeakReference<View> ᐝ;
    private int ᐝॱ;

    protected static class SavedState extends AbsSavedState {
        public static final Creator<SavedState> CREATOR = new ClassLoaderCreator<SavedState>() {
            public /* synthetic */ Object createFromParcel(Parcel parcel) {
                return ˏ(parcel);
            }

            public /* synthetic */ Object createFromParcel(Parcel parcel, ClassLoader classLoader) {
                return ˏ(parcel, classLoader);
            }

            public /* synthetic */ Object[] newArray(int i) {
                return ˎ(i);
            }

            public SavedState ˏ(Parcel parcel, ClassLoader classLoader) {
                return new SavedState(parcel, classLoader);
            }

            public SavedState ˏ(Parcel parcel) {
                return new SavedState(parcel, null);
            }

            public SavedState[] ˎ(int i) {
                return new SavedState[i];
            }
        };
        final int ˏ;

        public SavedState(Parcel parcel, ClassLoader classLoader) {
            super(parcel, classLoader);
            this.ˏ = parcel.readInt();
        }

        public SavedState(Parcelable parcelable, int i) {
            super(parcelable);
            this.ˏ = i;
        }

        public void writeToParcel(Parcel parcel, int i) {
            super.writeToParcel(parcel, i);
            parcel.writeInt(this.ˏ);
        }
    }

    @RestrictTo({androidx.annotation.RestrictTo.d.LIBRARY_GROUP})
    @Retention(RetentionPolicy.SOURCE)
    public @interface State {
    }

    public static abstract class c {
        public abstract void ˋ(@NonNull View view, int i);

        public abstract void ॱ(@NonNull View view, float f);
    }

    class d implements Runnable {
        private final int ˋ;
        private final View ˏ;
        final /* synthetic */ BottomSheetBehavior ॱ;

        d(BottomSheetBehavior bottomSheetBehavior, View view, int i) {
            this.ॱ = bottomSheetBehavior;
            this.ˏ = view;
            this.ˋ = i;
        }

        public void run() {
            if (this.ॱ.ʽ == null || !this.ॱ.ʽ.ˋ(true)) {
                this.ॱ.ˏ(this.ˋ);
            } else {
                ViewCompat.ˏ(this.ˏ, this);
            }
        }
    }

    public BottomSheetBehavior(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, n.ॱͺ);
        TypedValue peekValue = obtainStyledAttributes.peekValue(n.ॱʼ);
        if (peekValue == null || peekValue.data != -1) {
            ॱ(obtainStyledAttributes.getDimensionPixelSize(n.ॱʼ, -1));
        } else {
            ॱ(peekValue.data);
        }
        ˊ(obtainStyledAttributes.getBoolean(n.ॱʽ, false));
        ॱ(obtainStyledAttributes.getBoolean(n.ॱʻ, true));
        ˏ(obtainStyledAttributes.getBoolean(n.ـ, false));
        obtainStyledAttributes.recycle();
        this.ॱˊ = (float) ViewConfiguration.get(context).getScaledMaximumFlingVelocity();
    }

    public Parcelable ˎ(CoordinatorLayout coordinatorLayout, V v) {
        return new SavedState(super.ˎ(coordinatorLayout, v), this.ˎ);
    }

    public void ˊ(CoordinatorLayout coordinatorLayout, V v, Parcelable parcelable) {
        SavedState savedState = (SavedState) parcelable;
        super.ˊ(coordinatorLayout, v, savedState.ˊ());
        if (savedState.ˏ == 1 || savedState.ˏ == 2) {
            this.ˎ = 4;
        } else {
            this.ˎ = savedState.ˏ;
        }
    }

    public boolean ˎ(CoordinatorLayout coordinatorLayout, V v, int i) {
        if (ViewCompat.ॱˋ(coordinatorLayout) && !ViewCompat.ॱˋ(v)) {
            v.setFitsSystemWindows(true);
        }
        int top = v.getTop();
        coordinatorLayout.ˋ(v, i);
        this.ʼ = coordinatorLayout.getHeight();
        if (this.ͺ) {
            if (this.ॱˎ == 0) {
                this.ॱˎ = coordinatorLayout.getResources().getDimensionPixelSize(o.aqu.c.ʻ);
            }
            this.ᐝॱ = Math.max(this.ॱˎ, this.ʼ - ((coordinatorLayout.getWidth() * 9) / 16));
        } else {
            this.ᐝॱ = this.ˊॱ;
        }
        this.ˋ = Math.max(0, this.ʼ - v.getHeight());
        this.ˊ = this.ʼ / 2;
        ˊ();
        if (this.ˎ == 3) {
            ViewCompat.ʽ(v, ʼ());
        } else if (this.ˎ == 6) {
            ViewCompat.ʽ(v, this.ˊ);
        } else if (this.ॱ && this.ˎ == 5) {
            ViewCompat.ʽ(v, this.ʼ);
        } else if (this.ˎ == 4) {
            ViewCompat.ʽ(v, this.ˏ);
        } else if (this.ˎ == 1 || this.ˎ == 2) {
            ViewCompat.ʽ(v, top - v.getTop());
        }
        if (this.ʽ == null) {
            this.ʽ = jf.ˋ((ViewGroup) coordinatorLayout, this.ˉ);
        }
        this.ॱॱ = new WeakReference(v);
        this.ᐝ = new WeakReference(ˋ(v));
        return true;
    }

    public boolean ˎ(CoordinatorLayout coordinatorLayout, V v, MotionEvent motionEvent) {
        boolean z = true;
        if (v.isShown()) {
            View view;
            int actionMasked = motionEvent.getActionMasked();
            if (actionMasked == 0) {
                ˋ();
            }
            if (this.ʼॱ == null) {
                this.ʼॱ = VelocityTracker.obtain();
            }
            this.ʼॱ.addMovement(motionEvent);
            switch (actionMasked) {
                case 0:
                    int x = (int) motionEvent.getX();
                    this.ʽॱ = (int) motionEvent.getY();
                    view = this.ᐝ != null ? (View) this.ᐝ.get() : null;
                    if (view != null && coordinatorLayout.ˋ(view, x, this.ʽॱ)) {
                        this.ʻ = motionEvent.getPointerId(motionEvent.getActionIndex());
                        this.ˏॱ = true;
                    }
                    boolean z2 = this.ʻ == -1 && !coordinatorLayout.ˋ(v, x, this.ʽॱ);
                    this.ॱˋ = z2;
                    break;
                case 1:
                case 3:
                    this.ˏॱ = false;
                    this.ʻ = -1;
                    if (this.ॱˋ) {
                        this.ॱˋ = false;
                        return false;
                    }
                    break;
            }
            if (!this.ॱˋ && this.ʽ != null && this.ʽ.ˏ(motionEvent)) {
                return true;
            }
            if (this.ᐝ != null) {
                view = (View) this.ᐝ.get();
            } else {
                view = null;
            }
            if (actionMasked != 2 || r0 == null || this.ॱˋ || this.ˎ == 1 || coordinatorLayout.ˋ(r0, (int) motionEvent.getX(), (int) motionEvent.getY()) || this.ʽ == null || Math.abs(((float) this.ʽॱ) - motionEvent.getY()) <= ((float) this.ʽ.ˏ())) {
                z = false;
            }
            return z;
        }
        this.ॱˋ = true;
        return false;
    }

    public boolean ॱ(CoordinatorLayout coordinatorLayout, V v, MotionEvent motionEvent) {
        if (!v.isShown()) {
            return false;
        }
        int actionMasked = motionEvent.getActionMasked();
        if (this.ˎ == 1 && actionMasked == 0) {
            return true;
        }
        if (this.ʽ != null) {
            this.ʽ.ˋ(motionEvent);
        }
        if (actionMasked == 0) {
            ˋ();
        }
        if (this.ʼॱ == null) {
            this.ʼॱ = VelocityTracker.obtain();
        }
        this.ʼॱ.addMovement(motionEvent);
        if (actionMasked == 2 && !this.ॱˋ && Math.abs(((float) this.ʽॱ) - motionEvent.getY()) > ((float) this.ʽ.ˏ())) {
            this.ʽ.ॱ((View) v, motionEvent.getPointerId(motionEvent.getActionIndex()));
        }
        if (this.ॱˋ) {
            return false;
        }
        return true;
    }

    public boolean ˏ(@NonNull CoordinatorLayout coordinatorLayout, @NonNull V v, @NonNull View view, @NonNull View view2, int i, int i2) {
        this.ʻॱ = 0;
        this.ʾ = false;
        if ((i & 2) != 0) {
            return true;
        }
        return false;
    }

    public void ˎ(@NonNull CoordinatorLayout coordinatorLayout, @NonNull V v, @NonNull View view, int i, int i2, @NonNull int[] iArr, int i3) {
        if (i3 != 1 && view == ((View) this.ᐝ.get())) {
            int top = v.getTop();
            int i4 = top - i2;
            if (i2 > 0) {
                if (i4 < ʼ()) {
                    iArr[1] = top - ʼ();
                    ViewCompat.ʽ(v, -iArr[1]);
                    ˏ(3);
                } else {
                    iArr[1] = i2;
                    ViewCompat.ʽ(v, -i2);
                    ˏ(1);
                }
            } else if (i2 < 0 && !view.canScrollVertically(-1)) {
                if (i4 <= this.ˏ || this.ॱ) {
                    iArr[1] = i2;
                    ViewCompat.ʽ(v, -i2);
                    ˏ(1);
                } else {
                    iArr[1] = top - this.ˏ;
                    ViewCompat.ʽ(v, -iArr[1]);
                    ˏ(4);
                }
            }
            ˎ(v.getTop());
            this.ʻॱ = i2;
            this.ʾ = true;
        }
    }

    public void ॱ(@NonNull CoordinatorLayout coordinatorLayout, @NonNull V v, @NonNull View view, int i) {
        int i2 = 3;
        if (v.getTop() == ʼ()) {
            ˏ(3);
        } else if (view == this.ᐝ.get() && this.ʾ) {
            int ʼ;
            if (this.ʻॱ > 0) {
                ʼ = ʼ();
            } else if (this.ॱ && ˊ((View) v, ॱ())) {
                ʼ = this.ʼ;
                i2 = 5;
            } else if (this.ʻॱ == 0) {
                int top = v.getTop();
                if (this.ˋॱ) {
                    if (Math.abs(top - this.ˋ) < Math.abs(top - this.ˏ)) {
                        ʼ = this.ˋ;
                    } else {
                        ʼ = this.ˏ;
                        i2 = 4;
                    }
                } else if (top < this.ˊ) {
                    if (top < Math.abs(top - this.ˏ)) {
                        ʼ = 0;
                    } else {
                        ʼ = this.ˊ;
                        i2 = 6;
                    }
                } else if (Math.abs(top - this.ˊ) < Math.abs(top - this.ˏ)) {
                    ʼ = this.ˊ;
                    i2 = 6;
                } else {
                    ʼ = this.ˏ;
                    i2 = 4;
                }
            } else {
                ʼ = this.ˏ;
                i2 = 4;
            }
            if (this.ʽ.ॱ((View) v, v.getLeft(), ʼ)) {
                ˏ(2);
                ViewCompat.ˏ(v, new d(this, v, i2));
            } else {
                ˏ(i2);
            }
            this.ʾ = false;
        }
    }

    public boolean ˏ(@NonNull CoordinatorLayout coordinatorLayout, @NonNull V v, @NonNull View view, float f, float f2) {
        return view == this.ᐝ.get() && (this.ˎ != 3 || super.ˏ(coordinatorLayout, (View) v, view, f, f2));
    }

    public void ॱ(boolean z) {
        if (this.ˋॱ != z) {
            this.ˋॱ = z;
            if (this.ॱॱ != null) {
                ˊ();
            }
            int i = (this.ˋॱ && this.ˎ == 6) ? 3 : this.ˎ;
            ˏ(i);
        }
    }

    public final void ॱ(int i) {
        boolean z = true;
        if (i == -1) {
            if (!this.ͺ) {
                this.ͺ = true;
            }
            z = false;
        } else {
            if (this.ͺ || this.ˊॱ != i) {
                this.ͺ = false;
                this.ˊॱ = Math.max(0, i);
                this.ˏ = this.ʼ - i;
            }
            z = false;
        }
        if (z && this.ˎ == 4 && this.ॱॱ != null) {
            View view = (View) this.ॱॱ.get();
            if (view != null) {
                view.requestLayout();
            }
        }
    }

    public void ˊ(boolean z) {
        this.ॱ = z;
    }

    public void ˏ(boolean z) {
        this.ॱᐝ = z;
    }

    public void ॱ(c cVar) {
        this.ˈ = cVar;
    }

    public final void ˊ(final int i) {
        if (i != this.ˎ) {
            if (this.ॱॱ != null) {
                final View view = (View) this.ॱॱ.get();
                if (view != null) {
                    ViewParent parent = view.getParent();
                    if (parent != null && parent.isLayoutRequested() && ViewCompat.ˋˊ(view)) {
                        view.post(new Runnable(this) {
                            final /* synthetic */ BottomSheetBehavior ˏ;

                            public void run() {
                                this.ˏ.ˊ(view, i);
                            }
                        });
                    } else {
                        ˊ(view, i);
                    }
                }
            } else if (i == 4 || i == 3 || i == 6 || (this.ॱ && i == 5)) {
                this.ˎ = i;
            }
        }
    }

    public final int ˏ() {
        return this.ˎ;
    }

    void ˏ(int i) {
        if (this.ˎ != i) {
            this.ˎ = i;
            if (i == 6 || i == 3) {
                ˎ(true);
            } else if (i == 5 || i == 4) {
                ˎ(false);
            }
            View view = (View) this.ॱॱ.get();
            if (view != null && this.ˈ != null) {
                this.ˈ.ˋ(view, i);
            }
        }
    }

    private void ˊ() {
        if (this.ˋॱ) {
            this.ˏ = Math.max(this.ʼ - this.ᐝॱ, this.ˋ);
        } else {
            this.ˏ = this.ʼ - this.ᐝॱ;
        }
    }

    private void ˋ() {
        this.ʻ = -1;
        if (this.ʼॱ != null) {
            this.ʼॱ.recycle();
            this.ʼॱ = null;
        }
    }

    boolean ˊ(View view, float f) {
        if (this.ॱᐝ) {
            return true;
        }
        if (view.getTop() < this.ˏ) {
            return false;
        }
        if (Math.abs((((float) view.getTop()) + (BarrierParameters.RESTITUTION * f)) - ((float) this.ˏ)) / ((float) this.ˊॱ) <= 0.5f) {
            return false;
        }
        return true;
    }

    @VisibleForTesting
    View ˋ(View view) {
        if (ViewCompat.ˈ(view)) {
            return view;
        }
        if (view instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view;
            int childCount = viewGroup.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View ˋ = ˋ(viewGroup.getChildAt(i));
                if (ˋ != null) {
                    return ˋ;
                }
            }
        }
        return null;
    }

    private float ॱ() {
        if (this.ʼॱ == null) {
            return 0.0f;
        }
        this.ʼॱ.computeCurrentVelocity(1000, this.ॱˊ);
        return this.ʼॱ.getYVelocity(this.ʻ);
    }

    private int ʼ() {
        return this.ˋॱ ? this.ˋ : 0;
    }

    void ˊ(View view, int i) {
        int i2;
        if (i == 4) {
            i2 = this.ˏ;
        } else if (i == 6) {
            i2 = this.ˊ;
            if (this.ˋॱ && i2 <= this.ˋ) {
                i2 = this.ˋ;
                i = 3;
            }
        } else if (i == 3) {
            i2 = ʼ();
        } else if (this.ॱ && i == 5) {
            i2 = this.ʼ;
        } else {
            throw new IllegalArgumentException("Illegal state argument: " + i);
        }
        if (this.ʽ.ॱ(view, view.getLeft(), i2)) {
            ˏ(2);
            ViewCompat.ˏ(view, new d(this, view, i));
            return;
        }
        ˏ(i);
    }

    void ˎ(int i) {
        View view = (View) this.ॱॱ.get();
        if (view != null && this.ˈ != null) {
            if (i > this.ˏ) {
                this.ˈ.ॱ(view, ((float) (this.ˏ - i)) / ((float) (this.ʼ - this.ˏ)));
            } else {
                this.ˈ.ॱ(view, ((float) (this.ˏ - i)) / ((float) (this.ˏ - ʼ())));
            }
        }
    }

    public static <V extends View> BottomSheetBehavior<V> ॱ(V v) {
        LayoutParams layoutParams = v.getLayoutParams();
        if (layoutParams instanceof a) {
            Behavior ˋ = ((a) layoutParams).ˋ();
            if (ˋ instanceof BottomSheetBehavior) {
                return (BottomSheetBehavior) ˋ;
            }
            throw new IllegalArgumentException("The view is not associated with BottomSheetBehavior");
        }
        throw new IllegalArgumentException("The view is not a child of CoordinatorLayout");
    }

    private void ˎ(boolean z) {
        if (this.ॱॱ != null) {
            ViewParent parent = ((View) this.ॱॱ.get()).getParent();
            if (parent instanceof CoordinatorLayout) {
                CoordinatorLayout coordinatorLayout = (CoordinatorLayout) parent;
                int childCount = coordinatorLayout.getChildCount();
                if (VERSION.SDK_INT >= 16 && z) {
                    if (this.ʿ == null) {
                        this.ʿ = new HashMap(childCount);
                    } else {
                        return;
                    }
                }
                for (int i = 0; i < childCount; i++) {
                    View childAt = coordinatorLayout.getChildAt(i);
                    if (childAt != this.ॱॱ.get()) {
                        if (z) {
                            if (VERSION.SDK_INT >= 16) {
                                this.ʿ.put(childAt, Integer.valueOf(childAt.getImportantForAccessibility()));
                            }
                            ViewCompat.setImportantForAccessibility(childAt, 4);
                        } else if (this.ʿ != null && this.ʿ.containsKey(childAt)) {
                            ViewCompat.setImportantForAccessibility(childAt, ((Integer) this.ʿ.get(childAt)).intValue());
                        }
                    }
                }
                if (!z) {
                    this.ʿ = null;
                }
            }
        }
    }
}
